package dp;

/**
 * 面试题 05.03. 翻转数位
 */
public class P0503 {
    public int reverseBits(int num) {
        if (num == 0) {
            return 1;
        }

        String numStr = Integer.toBinaryString(num);
        if (num > 0) {
            numStr = "0" + numStr;
        }

        int[][] count = new int[numStr.length()][2];


        if (numStr.charAt(0) == '0') {
            count[0][0] = 0;
            count[0][1] = 1;
        } else {
            count[0][0] = 1;
            count[0][1] = 1;
        }
        int max = 1;
        for (int i = 1; i < numStr.length(); i++) {
            char c = numStr.charAt(i);
            if (c == '0') {
                count[i][0] = 0;
                count[i][1] = count[i - 1][0] + 1;
            } else if (c == '1') {
                count[i][0] = count[i - 1][0] + 1;
                count[i][1] = count[i - 1][1] + 1;
            }
            int now = Math.max(count[i][0], count[i][1]);
            if (now > max) {
                max = now;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int i = new P0503().reverseBits(-2);
        System.out.println(i);
        Math.max(1, 2);
    }
}
